package com.ijiuchuang.jc_localization.tools.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.ijiuchuang.jc_localization.R;

/**
 * Created by Veev on 2016/6/12
 *
 * @link 384506557@qq.com
 */
public class LocationView extends View {

    // 节点名称
    private String pointName = "";
    // 文字大小
    private int textSize = 12;

    // 画笔
    private Paint mPaint;
    private Paint textPaint;

    // 文字属性
    private Paint.FontMetrics fm;

    // 定位图标的bitmap
    private Bitmap mBitmap;

    public LocationView(Context context) {
        super(context);

        init();
    }

    public LocationView(Context context, String pointName) {
        super(context);
        this.pointName = pointName;

        init();
    }

    public LocationView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LocationView, 0, 0);

        try {
            pointName = ta.getString(R.styleable.LocationView_point_name);
            textSize = (int) ta.getDimension(R.styleable.LocationView_point_text_size, 36) / 3;
        } finally {
            ta.recycle();
        }

        init();
    }

    public LocationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init();
    }

    private void init() {
        mPaint = new Paint();
        textPaint = new Paint();

        mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic_location_aa);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setAlpha(255);
        canvas.drawBitmap(mBitmap, 0, 0, mPaint);

        textPaint.setColor(Color.BLACK);
        textPaint.setFakeBoldText(true);
        textPaint.setTextSize(textSize);
        fm = textPaint.getFontMetrics();
        // 文本的宽度
        float textWidth = textPaint.measureText(pointName);
        // 文字的X坐标
        float textX = (mBitmap.getWidth() - textWidth) / 2;
        // 文字的Y坐标
        float textY = mBitmap.getHeight() + fm.descent + (fm.descent - fm.ascent) / 2;

        mPaint.setColor(Color.WHITE);
        mPaint.setAlpha(32);
        canvas.drawRect(textX, mBitmap.getHeight(), textX + textWidth, textY, mPaint);
        canvas.drawText(pointName, textX, textY, textPaint);
    }

    /**
     * 设置节点名称
     * @param pointName 节点名称
     */
    public void setPointName(String pointName) {
        this.pointName = pointName;
        // 重绘
        invalidate();
    }

    /**
     * 获取节点名称
     * @return 节点名称
     */
    public String getPointName() {
        return pointName;
    }

    /**
     * 设置节点文字大小
     * @param textSize 文字大小
     */
    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }
}
